<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>对象</title>
    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="module">

        //对象声明法
        var student = {
            name: "Eden",
            printName: function () {
                console.log(this.name);
            }
        }
        student.printName(); // Eden

        //用构造函数创建对象
        function Student(name) {
            this.name = name;
        }
        Student.prototype.printName = function () {
            console.log(this.name);
        }
        var student1 = new Student("Eden");
        var student2 = new Student("John");
        student1.printName(); // "Eden"
        student2.printName(); // "John"

        //给对象自定义原型继承
        var object1 = {
            name: "Eden",
            __proto__: {age: 24}
        }
        var object2 = {name: "Eden"}
        Object.setPrototypeOf(object2, {age: 24});
        var object3 = Object.create({age: 24}, {name: {value: "Eden"}});
        console.log(object1.name + " " + object1.age);  //Eden 24
        console.log(object2.name + " " + object2.age);  // Eden 24
        console.log(object3.name + " " + object3.age);  // Eedn 24


        //使用对象声明方式创建对象时不分配原型，那么原型将指向Object.prototype属性
        var obj = {
            name: "Eden"
        }
        console.log(obj.__proto__ == Object.prototype); //Output "true"

        //使用构造函数创建对象时，新对象的原型始终指向对象的一个名为prototype属性
        function Student() {
            this.name = "Eden";
        }
        var obj = new Student();
        console.log(obj.__proto__.constructor == Student); // "true"
        console.log(obj.__proto__ == Student.prototype); // "true"

        //新的方法添加到构造函数的prototype属性中，构造函数的每个实例都将具有方法的副本,
        //所有实例共享的每个函数只有一个副本
        function Student(name) {
            this.name = name;
        }
        Student.prototype.printName = function () {
            console.log(this.name);
        }
        var s1 = new Student("Eden");
        var s2 = new Student("John");
        console.log(s1.printName == s2.printName); //true
        function School(name) {
            this.name = name;
            this.printName = function () {
                console.log(this.name);
            }
        }
        var s3 = new School("ABC");
        var s4 = new School("XYZ");
        console.log(s3.printName == s4.printName); //false


        //继承
        //先用构造函数创建一个父对象
        function School(schoolName) {
            this.schoolName = schoolName;
        }
        School.prototype.printSchoolName = function () {
            console.log(this.schoolName);
        }
        function Student(studentName, schoolName) {
            this.studentName = studentName;
            //将参数传递给子类的继承中
            School.call(this, schoolName);
        }
        //继承父类
        Student.prototype = new School();
        Student.prototype.printStudentName = function () {
            console.log(this.studentName);
        }
        var s = new Student("Eden", "ABC School");
        s.printStudentName(); // Eden
        s.printSchoolName();  // ABC School

    </script>
</head>
<body>

</body>
</html>